<?php

if (isset($_GET['idRes']) && isset($_GET['week']) && isset($_GET['session']) && isset($_GET['jour']))
{
	$idRes = $_GET['idRes'];
	$week = $_GET['week'];
	$session = $_GET['session'];
	$jour = $_GET['jour'];
}
if (isset($idRes) && isset($week) && isset($session) && isset($jour))
{
	function cmpTabEvent($a, $b)
	{
		return ($a->attributes->item(6)->value < $b->attributes->item(6)->value) ? -1 : 1;
	}
		
	$erreurRecup = false;
	if ($session == -1)
		$erreurRecup = true;
	if (!$erreurRecup)
	{
		$url = "http://ade52-savoie.grenet.fr/ade/webapi?sessionId=$session&function=getEvents&resources=$idRes&detail=0&weeks=$week&days=$jour"; //getEvents&resources=1165&detail=4&weeks=12&days=0
		$agent = '';
		$id = '';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_USERAGENT, $agent);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 4);   
		curl_setopt($ch, CURLOPT_COOKIE, $id);
	 
		$ret = curl_exec($ch);
		if (curl_errno($ch))
		{
			$erreurRecup = true;
		}
		else
			curl_close($ch);
		
		if (!$erreurRecup &&(strpos($ret, '<event') === false))
		{
			$erreurRecup = true;
		}
	} 
	
	if ($erreurRecup && $idRes != ""  )
	{
		require("connection_mysql.inc.php");
		$requete = "SELECT xml from sauvegarde where id = $idRes AND semaine = $week AND jour = $jour";
		echo "<!-- $requete -->";
		$resultRequete = mysql_query($requete) or die(mysql_query());
		$result = mysql_fetch_array($resultRequete);
		if ($result)
			$ret = $result['xml'];
		else if ($jour == 0)
			echo "<p>Impossible de r&eacute;cup&eacute;rer cette semaine depuis la base.</p>";
	}
	if ($ret)
	{
		$page = new DOMDocument();
		$page->loadXML($ret);
			
		
		$heureActuelle = 2; //correspond a 7h30
		$events = $page->getElementsByTagName("event");
		$tabEvent = array();
		for ($j = 0; $j < $events->length; $j++)
		{
			$tabEvent[$j] = $events->item($j);
		}
		usort( $tabEvent , "cmpTabEvent" );
		
		
		$weekBis = $week + 32;
		if ($weekBis > 52) $weekBis -= 52;
		if ((date('w') == ($jour+1) && $weekBis == date('W')) || ($weekBis == date('W')+1 && (date('w') == 0 || date('w') == 6) && ($jour+1) == 1 ))
			echo "<div class='jour_rouge2'>";
		for ($j = 0; $j < count($tabEvent); $j++)
		{
			$detailCours = $tabEvent[$j]->attributes->getNamedItem("name")->value ; // 4 - Nom du cours
			$heureDebut = $tabEvent[$j]->attributes->getNamedItem('slot')->value ; // 6 - Heure du cours
			$duree = $tabEvent[$j]->attributes->getNamedItem('duration')->value ; // 10 - DurÃ©e du cours
			$idActivity = $tabEvent[$j]->attributes->item(1)->value;
			$couleur = $tabEvent[$j]->attributes->getNamedItem('color')->value;
			
			$heureDebutTot = $heureDebut * 1/4 + 7.5;
			$heureDebutH = floor($heureDebutTot);
			$heureDebutM = ($heureDebutTot - $heureDebutH) * 60;
			
			$heureFinTot = $heureDebutTot + $duree * 1/4;
			$heureFinH = floor($heureFinTot);
			$heureFinM = ($heureFinTot - $heureFinH) * 60;
			
			$ressourcesTemp = $tabEvent[$j]->childNodes->item(1);
			$ressources = $ressourcesTemp->childNodes;
			
			$prof = '';
			$salle = '';
			foreach ($ressources as $ressource)
			{
				if ($ressource->hasAttributes())
				{
					if ($ressource->attributes->item(4)->value == 'instructor')
						$prof = $ressource->attributes->item(5)->value;
					if ($ressource->attributes->item(4)->value == 'classroom')
						$salle = $ressource->attributes->item(5)->value;
				}
				
			}
			
			
			if ($heureActuelle < $heureDebut)
			{
				echo "<div class='zoneEdtBlanc' style=' height:".(($heureDebut-$heureActuelle) *15 )."px' ></div>";
			}
			echo "<div class='zoneEdt' style='";
			if ($duree <= 3 ) echo "font-size:0.8em; ";
			echo "background-color:rgb($couleur);height:".(($duree) *15 )."px' >";
			if ($duree > 3 ) echo "<div class='infosImage'></div><div style='float:left;' class='toto'>";
			else echo "<div style='' class='toto'>";
			echo "<p";
			if ($duree <= 3) echo " style='padding-top:0px;padding-bottom:0px;text-align:center;' ";
			echo ">$detailCours<br/>";
			echo $heureDebutH."h".(($heureDebutM > 0)? $heureDebutM:"")." - ".$heureFinH."h".(($heureFinM > 0)? $heureFinM:"")." : ".$salle;
			if ($duree > 3 ) echo "<br/>$prof";
			echo "</p>";
			echo "<input type='hidden' value='$idActivity' />";
			echo "</div></div>";
			$heureActuelle = $heureDebut + $duree;
		}
		echo "</div>";
	}
}
?>


















